package edu.cmu.cs.stage3.alice.core.visualization;

import edu.cmu.cs.stage3.alice.core.Model;
import edu.cmu.cs.stage3.alice.core.Variable;

public class ModelVisualization extends edu.cmu.cs.stage3.alice.core.Visualization {
	//Rem @Override
	public void unhook( Model model ) {
		if( getItem()==model ) {
			setItem( null );
		}
	}
	private Variable m_itemVariable = null;
	private Variable getItemVariable() {
		if( m_itemVariable == null ) {
			m_itemVariable = (Variable)getChildNamed( "Item" );
		}
		return m_itemVariable;
	}
	public Model getItem() {
		return (Model)getItemVariable().value.getValue();
	}
	public void setItem( Model value ) {
		getItemVariable().value.set( value );
	}
	private void synchronize( Model curr ) {
		Model prev = getItem();
		if( prev != null && prev != curr ) {
			prev.visualization.set( null );
		}
	}
	//Rem @Override
	protected void loadCompleted() {
		super.loadCompleted();
		Variable item = getItemVariable();
		item.value.addPropertyListener( new edu.cmu.cs.stage3.alice.core.event.PropertyListener() {
			public void propertyChanging( edu.cmu.cs.stage3.alice.core.event.PropertyEvent propertyEvent ) {
			}
			public void propertyChanged( edu.cmu.cs.stage3.alice.core.event.PropertyEvent propertyEvent ) {
				ModelVisualization.this.synchronize( (Model)propertyEvent.getValue() );
			}
		} );
		synchronize( getItem() );
	}
}
